<template>
    <div>
        <a-date-picker v-model="localDate" show-time
                       :format="format"
        />
        <a-checkbox v-model="localChecked" @change="changeDate">{{i18n('form.date','日期')}}</a-checkbox>
    </div>
</template>

<script>
    import moment from "moment";
    export default {
        name: "rx-form-date",
        props:{
            value:{
                type:String
            }
        },
        data(){
            return {
                localDate:"",
                format:"YYYY-MM-DD",
                localChecked:true,

                longFormat:"YYYY-MM-DD HH:mm:ss",
                shortFormat:"YYYY-MM-DD",
            }
        },
        created() {
            if(this.value){
                this.localChecked=this.value.length==10;
                this.format= this.localChecked?this.shortFormat:this.longFormat;
                this.localDate=moment(this.value,this.format);
            }
        },
        methods:{
            i18n(name,text,key){
                return this.$ti18(name,text,"formCellRender",key);
            },
            changeDate(e){
                this.localChecked=e.target.checked;
                this.format=this.localChecked?this.shortFormat:this.longFormat;

                this.emitVal();
            },
            emitVal(){
                if(this.localDate){
                    var tmp=this.localDate.format(this.format);
                    this.$emit("input",tmp) ;
                }
                else{
                    this.$emit("input","") ;
                }
            }
        },
        watch:{
            localDate:function (val,oldVal) {
                this.emitVal();
            }
        }

    }
</script>

<style scoped>

</style>